package com;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @auther Macrydy
 * @date 2020/7/17 10:17
 */
public class UploadClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",10086);

        String s = null;
        List<String> list = new ArrayList<>();
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入上传文件的路径：");
            s = sc.nextLine();
            File file = new File("D:\\");
            File[] files = file.listFiles();
            for (File file1 : files) {
                list.add("file1");
            }
            String[] split = s.split("\\.");
            if ("jpg".equals(split[1])){
                    if (list.contains(s)){
                        break;
                    }else {
                        System.out.println("请输入正确的图片文件路径，请重新输入");
                    }
            }else {
                System.out.println("请输入正确的图片文件路径，请重新输入");
            }
        }

        //读取文件
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s));
        //写到服务器
        OutputStream os = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);

        int b;
        while((b = bis.read())!=-1){
            bos.write(b);
        }
        bos.flush();
        //结束标记
        socket.shutdownOutput();

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        while((line = br.readLine()) !=null){
            System.out.println(line);
        }
        bis.close();
        socket.close();
    }
}
